﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using log4net;
using RealState.BusinessObjects;
using RealState.BLL;
using System.Collections.Generic;
using RealState.Utils;

namespace RealState.W
{
    public partial class RS : System.Web.UI.MasterPage
    {
        protected ILog log = LogManager.GetLogger("MasterPage");
        private Images imageBO;
        private ImagesBLL imageBLL;
        private Users usersBO;
        private UsersBLL userBLL;
        private List<Users> listUsers;
        private List<Images> listImage;
        private SupportsBLL supportBLL;
        protected void Page_Load(object sender, EventArgs e)
        {
            string userid = Convert.ToString(Session["CustomerID"]);
            lblFullname.Text = Convert.ToString(Session["FullName"]);
            if (!string.IsNullOrEmpty(userid))
            {
                login_tag.Visible = false;
                profile_tag.Visible = true;
                h5Login.Visible = true;
                lblLogin.Text = "Thông tin cá nhân";
            }
            else
            {
                login_tag.Visible = true;
                h5Login.Visible = false;
                profile_tag.Visible = false;
            }
            if (!Page.IsPostBack)
            {
                LoadImages();
                LoadSupport();
                 
            }

           
        }

        public void LoadSupport()
        {
            supportBLL = new SupportsBLL();
            try
            {
                RepeaterSupport.DataSource = supportBLL.GetSupportss();
                RepeaterSupport.DataBind();
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        public void LoadImages()
        {
            imageBO = new Images();
            imageBLL = new ImagesBLL();
            List<Images> listAdvs = new List<Images>();
            try
            {
                listImage = imageBLL.GetBanner();

                foreach (Images item in listImage)
                {
                    if (item.ImgType == 2)
                    {
                        ImageTopBanner.ImageUrl = item.ImgPath;
                        ImageTopBanner.AlternateText = item.Title;
                        HyperLink1.Target = "_blank";
                        HyperLink1.NavigateUrl = item.ClickUrl;
                    }
                    if (item.ImgType == 1)
                    {
                        ImageLogo.ImageUrl = item.ImgPath;
                        ImageLogo.AlternateText = item.Title;
                        ImageLogo.PostBackUrl = "~/Default.aspx";
                    }
                    if (item.ImgType == 3)
                    {
                        imageBO.Id = item.Id;
                        imageBO.ImgPath = item.ImgPath;
                        imageBO.ImgType = item.ImgType;
                        imageBO.IsActive = item.IsActive;
                        imageBO.RowNum = item.RowNum;
                        imageBO.Sorted = item.Sorted;
                        imageBO.Title = item.Title;
                        imageBO.ClickUrl = item.ClickUrl;
                        listAdvs.Add(imageBO);
                    }
                }
                RepeaterAdvs.DataSource = listAdvs;
                RepeaterAdvs.DataBind();
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            userBLL = new UsersBLL();
            usersBO = new Users();
            try
            {
                usersBO.UserName = txtUserName.Text;
                usersBO.Password = Converter.MD5Encrypt(txtPassword.Text);

                listUsers = userBLL.GetUserss().Where(u => u.UserName == usersBO.UserName && u.Password == usersBO.Password).ToList();
                if (listUsers.Count == 1)
                {
                    Session.Add("CustomerID", listUsers[0].Id);
                    Session.Add("FullName", listUsers[0].FullName);
                    Session.Add("Password",listUsers[0].Password);
                    lblFullname.Text = Convert.ToString(Session["FullName"]);
                    Session.Add("UsersBO", listUsers[0]);
                    Session.Timeout = 20;
                    login_tag.Visible = false;
                    profile_tag.Visible = true;
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["CustomerID"])))
            {
                ShowMsg("Bạn phải đăng nhập trước khi đăng tin!");
                return;
            }
            else
            {
                Response.Redirect("AddContent.aspx");
            }
        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        public static void ShowMsg(string message)
        {
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type='text/javascript'>alert('" + cleanMessage + "');</script>";
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(RS), "alert", script);
            }
        }
    }
}
